package com.twu.biblioteca.main;



import com.twu.biblioteca.domain.*;
import com.twu.biblioteca.main.menu.MenuItem;
import com.twu.biblioteca.util.Console;

import java.util.ArrayList;
import java.util.List;

public class App {


    private final Library library;
    private final Console console;
    private final UserManager users;

    private User user;


    public App(Library bookLibrary, Console console, UserManager users) {

        this.library = bookLibrary;
        this.console = console;
        this.users = users;

    }
    public Console getConsole(){
        return console;
    }

    public void start() {
        displayWelcomeMessage();
        MenuItem userMenuItemSelection;
        do{
            displayMenu();
             userMenuItemSelection = getUserMenuSelection();
            if(userMenuItemSelection !=null)
                performMenuAction(userMenuItemSelection);
            else
                displayErrorMessage();
        }while(userMenuItemSelection!=MenuItem.EXIT);
    }


    private void displayWelcomeMessage() {
        //To change body of created methods use File | Settings | File Templates.
        console.println("Welcome to Bangalore Public Library");
    }

    private void displayErrorMessage() {
        //To change body of created methods use File | Settings | File Templates.
        console.println("Please select the right option");
    }

    private void performMenuAction(MenuItem userMenuItemSelection) {
        userMenuItemSelection.performAction(this);
    }

    private MenuItem getUserMenuSelection() {
        //TODO : Read from console and get corresponding MenuItem instance. You can get the instance by using MenuItem.itemFor(<itemId from user>)

        return MenuItem.itemFor(Integer.parseInt(console.getUserInput()));
    }

    private void displayMenu() {
        final MenuItem[] options = MenuItem.values();
        console.println("Main Menu");
        for (MenuItem option : options) {
            displayItem(option);
        }
        console.println("Please select any of the options: ");
    }


    public void displayAllBooks(){
        List<Displayable> books= new ArrayList<Displayable>();
        books = library.getAllBooks();

       console.printList(books);
    }

    public void reserveBook(){
        if(user!=null){
        Integer bookId;
        console.println("Please enter the book id for the book to be reserved");
        bookId=Integer.parseInt(console.getUserInput());


            boolean b = library.issueBook(bookId,user);


            if(b){
                user.issueBook(bookId);
                console.println("Thank You! Enjoy the book.");
            }
            else
                console.println("Sorry!! We were unable to reserve right now.");

        }
        else
            console.println("Please login before reserving");

    }

    public void exit(){
        //runtime.exit(0);

    }

    private void displayItem(MenuItem option) {
        console.println(String.format("%s: %s", option.getMenuItemId(), option.getMenuItemDesc()));
    }

    public static void main(String args[]){
        List<Book> books=new ArrayList<Book>();
        Book book=new Book(1,"Harry Potter and Goblet of Fire","JK Rowling","Bloomsbury",2007,10,0);
        books.add(book);
        book=new Book(2,"The Men who killed Gandhi","Manohar Malgonkar","Roli Books Pvt. Ltd.",2009,2,0);
        books.add(book);
        book=new Book(3,"Living with Honour","Shiv Khera","Macmillan India",2003,1,1);
        books.add(book);
        book=new Book(4,"Mahatma Gandhi His Life & Times", "Louis Fischer","Bharatiya Vidya Bhavan",2006,2,0);
        books.add(book);

        ArrayList<Movie> movies=new ArrayList<Movie>();
        Movie movie=new Movie(1, "Sholay", 1975, "Ramesh Sippy", null);
        movies.add(movie);
        movie=new Movie(2, "Kal ho na ho", 2003, "Karon Johar", 6);
        movies.add(movie);
        movie=new Movie(3, "Dead Man Walking", 1995, " Tim Robbins", 9);
        movies.add(movie);
        movie=new Movie(4, "Mr. Holland's Opus", 1995, "Stephen Herek", 0);
        movies.add(movie);
        movie=new Movie(5, "Forrest Gump", 1994, "Robert Zemeckis", 10);
        movies.add(movie);
        movie=new Movie(6, "The Promotion", 2008, "Steve Conrad", 9);
        movies.add(movie);
        movie=new Movie(7, "17 Again", 2009, "Burr Steers", 8);
        movies.add(movie);

        final Library bookLibrary = new Library(books,movies);
        final Console console = new Console(System.in, System.out);

        final UserManager users=new UserManager();
        for(int i=0;i<10;i++){
            users.addUser("Userlibrary","sample00"+i+"@tw.com" ,1234567+i );
        }


        final App app = new App(bookLibrary,console, users);
        //long i=1111111;
       //  String passWord=console.getPasswordInput();
           //console.println(passWord);
        app.start();

    }


    public void checkMembership() {
        //To change body of created methods use File | Settings | File Templates.
        if(user!=null){
           console.println(user.getDisplayText());
        }
        else
        console.println("Please talk to librarian");
    }

    public void login() {
        //To change body of created methods use File | Settings | File Templates.
        console.println("Enter the username: ");
        String userName=console.getUserInput();
        console.println("Enter the password: ");
        String passWord=console.getUserInput();

        setUser(users.checkUserLogin(userName,passWord));


    }

    private void setUser(User user) {
        if(user!=null)
        {   console.println("Successfully logged in");
            this.user=user;
        }
        else
            console.println("Invalid Entry");
    }

    public void displayAllMovies() {
        //To change body of created methods use File | Settings | File Templates.
       List<Displayable> movies=library.getAllMovies();
       console.printList(movies);
    }
}




